package com.itranswarp.learnjava;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Learn Java from https://www.liaoxuefeng.com/
 * 
 * @author liaoxuefeng
 */
public class Main {

	public static void main(String[] args) throws Exception {
		String name = "Xiao Ming";
		int age = 20;
		Person p = new Person();
		// TODO: 利用反射调用setName和setAge方法:
		Class pClass=p.getClass();
		Method mSetName=pClass.getMethod("setName", String.class);
		mSetName.invoke(p,name);

		Method mSetAge=pClass.getMethod("setAge", int.class);
		mSetAge.invoke(p,age);
		System.out.println(p.getName()); // "Xiao Ming"
		System.out.println(p.getAge()); // 20


		Constructor cons1=Integer.class.getConstructor(int.class);

		Integer n1=(Integer)cons1.newInstance(20);
		System.out.println(n1);

		Class sClass=Integer.class;
		Class[] is=sClass.getInterfaces();
		for(Class i:is){
			System.out.println(i);
		}


		InvocationHandler handler=new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				System.out.println(method);
				if(method.getName().equals("hello")){
					System.out.println("Hello");
				}
				return null;
			}
		};
		
		Hello hello= (Hello) Proxy.newProxyInstance(Hello.class.getClassLoader(),
				new Class[]{Hello.class},
				handler);
		hello.hello();
	}
}
